# source/bst/modCalculatable.py

# Calculatable interface
# Ben Terris
# 11/20/2007
from modCalculatable import Calculatable
from modCalculatablePolynomial import CalculatablePolynomial

class CalculatableNumber(Calculatable):
        """Required methods to be usable in our calculator. Implementations overriding 
        these methods must be provided by each class. All operations return a new object without changing the
        original objects. Errors and unsupported operations return None."""

        def __init__(self, value):
                self.value = value

        def __add__(self, other):
                '''Produce a sum of the lhs and rhs objects.'''
                if isinstance(other, CalculatableNumber):
                        return CalculatableNumber(self.value + other.getVal())
                else:
                        return None
        
        def __sub__(self, other):
                '''Produce the difference of the lhs and rhs objects.'''
                return self.value - other

        def __negate__(self):
                '''Return the negative of this object.'''
                return CalculatableNumber(0-self.value)
        
        def __div__(self, other):
                 '''Produce the result of dividing the lhs and rhs objects.'''
                 return self.value / other
        
        def __mul__(self, other):
                '''Produce the product of the lhs and rhs objects.'''
                if isinstance(other, CalculatableNumber):
                        return CalculatableNumber(self.value * other.getVal())
                else:
                        return None

        def __invert__(self):
                '''Invert this object.'''
                if self.value == 0:
                        return None
                else:
                        return CalculatableNumber(1.0/self.value)
        
        def __square__(self):
                '''Return the square of this object.'''
                return self.value*self.value

        def __str__(self):
                '''Return the string value of this Calculatable ojbect.'''
                return self.value.__str__()

        def getVal(self):
                '''returns the origianal value'''
                return self.value


